clear
disp(' ')
data_dir = uigetdir([],'please locate a folder for a task ');
data_dir(end+1)=filesep;
datafolder=[data_dir '_blink_files\'];
if exist(datafolder,'dir')==0
	mkdir(datafolder)
end
cd(data_dir)
folders=dir;
folders(1:2)=[];
fold_name={folders.name}';
folders(strmatch('_',fold_name))=[];
fold_name={folders.name}';

for fold=1:length(folders)
	% 	if exist([datafolder folders(fold).name '.mat'],'file')==2
	% 		continue
	% 	end
	
	cd([data_dir folders(fold).name])
	edffile=dir('*.edf');
	if isempty(edffile)
		continue
	end
	tic
	E=el2mat_b2(edffile(1).name);
	fprintf('loading %s took: %4.2f seconds\n',edffile(1).name,toc)
	t=E.sampledata(:,1);
	x=E.sampledata(:,2);x(abs(x)>10000000)=nan;x=(x-(E.res.pix_wide/2));
	y=E.sampledata(:,3);y(abs(y)>10000000)=nan;y=(y-(E.res.pix_high/2));
	a=E.sampledata(:,4);
	%% task specific
	manual_override=false;
	SEND_SCREEN_METRICS=E.messages(findincell(E.messages,'disttoscreen'));
	if isempty(SEND_SCREEN_METRICS)
		manual_override=true;
	else
		SEND_SCREEN_METRICS=SEND_SCREEN_METRICS{1};
		fred=textscan(SEND_SCREEN_METRICS,repmat('%s %s ',1,(length(findstr(SEND_SCREEN_METRICS,' '))+1)/2));
		for i = 1:2:length(fred)
			eval(['E.res.' lower(char(fred{i})) '=' char(fred{i+1}) ';'])
		end
		if ~isfield(E.res,'swidthinches') || ~isfield(E.res,'disttoscreen')
			manual_override=true;
		end
	end
	if manual_override
		E.res.swidthinches=13;
		E.res.disttoscreen=23;
		disp('couldn''t find SEND_SCREEN_METRICS, using default 13inch screen viewed from 23inches away')		
	end
	
	dphs=atan((E.res.swidthinches/2)/E.res.disttoscreen)*180/pi;% degrees per half of a 13inch screen from 23 inches away
	pixperdeg=(E.res.pix_wide/2)/dphs; % pixels per degree
	E.res.ppd=pixperdeg;
	x=x/pixperdeg;
	y=y/pixperdeg;
	%%
% 	figure(1);clf; hold on
% 	plot(t,x,'.-c')
% 	plot(t,y,'.-m')
% 	plot(t,a/50,'.-','color',[.8 .8 .8])
	tic;
	[ct cx cy ca xo yo ao csacloc csac_thresh cblink cspeed]=EL1k_blinkremover_sacdetect_trial(t, x, y, a, 1,2.5);
	fprintf('blink & saccade took: %4.2f seconds\n',toc)
	E=rmfield(E,'sampledata');% this data is recoverable from t, xo, yo, ao, E.t_adjust, & E.res.ppd
	E.eye.t=ct;
	E.eye.x=cx;
	E.eye.y=cy;
	E.eye.a=ca;
	E.eye.xo=xo; % original
	E.eye.yo=yo; % original
	E.eye.ao=ao; % original
	E.eye.sacloc=csacloc;
	E.eye.sac_thresh=single(csac_thresh);
	E.eye.blink=cblink;
	E.eye.speed=cspeed;

	cd(datafolder)
	save([folders(fold).name '.mat'],'E');
	cd(data_dir)

	%%
%	tic
% 	for tr = 1:length(cx)
% 		loc(tr)=length(cx{tr}); %#ok<SAGROW>
% 		h(1)=plot(ct{tr},cx{tr},'.-b');
% 		h(2)=plot(ct{tr},cy{tr},'.-r');
% 		h(3)=plot(ct{tr},ca{tr}/50,'.-k');
% 		h(4)=plot(ct{tr},cspeed{tr}/50+10,'.-g');
% 		h(5)=plot([min(ct{tr}) max(ct{tr})],[csac_thresh(tr) csac_thresh(tr)]/50+10,'color',[1 .5 0]);
% 		vfill(ct{tr}(csacloc{tr}(:,1)),ct{tr}(csacloc{tr}(:,2)),[1 .5 0],.5);
% 		%[msacloc{tr} msac_thres{tr}] = EL1k_microsacdetect(cspeed{tr}, 1, 2);
% 	end
% 	end_loc=cumsum(loc);
% 	starloc=[1 end_loc(1:end-1)+1];
% 	vfill(t(end_loc(1:end-1)),t(starloc(2:end)),[.5 .5 .5],.2)
% 	axis tight
% 	hline(50/50+10,'--k'); % 50dps sac_thres
% 	hline(0/50+10,'k'); % 0dps speed baseline
% 	fprintf('plotting took: %4.2f seconds\n',toc)
% 	legend(h,'HORZ','VERT','AREA/50','SPEED/50+10','SACCADE')
% 	figure(1);
%% save data
end